package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.RoleDOMapper;
import com.livepan.server.dataobject.RoleDO;
import com.livepan.server.dataobject.RoleDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.RoleModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.CharsetMapping;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleService {
    @Autowired
    private RoleDOMapper roleDOMapper;

    @Autowired
    private ValidatorImpl validator;

    public void list(PageModel pageModel) {
        PageHelper.startPage(pageModel.getPage(), pageModel.getSize());
        RoleDOExample roleDOExample = new RoleDOExample();
        List<RoleDO> roleDOList = roleDOMapper.selectByExample(roleDOExample);
        PageInfo<RoleDO> pageInfo = new PageInfo<>(roleDOList);
        pageModel.setTotal(pageInfo.getTotal());
        List<RoleModel> roleModelList = roleDOList.stream().map(
                this::convertRoleModelFromRoleDO
        ).collect(Collectors.toList());
        pageModel.setList(roleModelList);
    }

    public List<RoleModel> allRole() {
        RoleDOExample roleDOExample = new RoleDOExample();
        List<RoleDO> roleDOList = roleDOMapper.selectByExample(roleDOExample);
        roleDOExample.setOrderByClause("id asc");
        List<RoleModel> roleModelList = roleDOList.stream().map(
                this::convertRoleModelFromRoleDO
        ).collect(Collectors.toList());
        return roleModelList;
    }

    public void save(RoleModel roleModel) throws BusinessException {
        ValidationResult validateResult = validator.validate(roleModel);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        RoleDO roleDO = CopyUtil.copy(roleModel, RoleDO.class);
        if (roleDOMapper.selectByPrimaryKey(roleDO.getId()) == null) {
            this.insert(roleDO);
        } else {
            this.update(roleDO);
        }
    }

    private void insert(RoleDO roleDO) {
        roleDO.setId(UUIDUtil.getShortUUID());
        roleDOMapper.insert(roleDO);
    }

    private void update(RoleDO roleDO) {
        roleDOMapper.updateByPrimaryKey(roleDO);
    }

    public void delete(String id) {
        roleDOMapper.deleteByPrimaryKey(id);
    }


    private RoleModel convertRoleModelFromRoleDO(RoleDO roleDO) {
        if (roleDO == null) {
            return null;
        }
        RoleModel roleModel = new RoleModel();
        BeanUtils.copyProperties(roleDO, roleModel);
        return roleModel;
    }
}
